// Diagram class : Creates the panel and contains the menu items methods

package appModel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import appForm.AppToolBar;
import appPref.Para;

public class Diagram extends JPanel implements MouseListener, MouseMotionListener, ActionListener {

  private Para para = Para.getPara();

  private static final String BACKGROUND     = "image/backGround1.gif";
  private static final String BACKGROUND1    = "image/backGround1.gif";
  private static final String BACKGROUND2    = "image/backGround2.gif";
  private static final String BACKGROUND3    = "image/backGround3.gif";
  private static final String PICTURE1       = "image/biblio2.gif";
  private static final String PICTURE2       = "image/bureau.gif";
  private static final String PICTURE3       = "image/chaise4.gif";
  private static final String PICTURE4       = "image/table1.gif";
  private static final String PICTURE5       = "image/chaise1.gif";
  private static final String PICTURE6       = "image/fauteuil2.gif";
  private static final String PICTURE7       = "image/fauteuil3.gif";
  private static final String PICTURE8       = "image/stereo.gif";
  private static final String PICTURE9       = "image/tv2.gif";
  private static final String PICTURE10      = "image/porte.gif";
  private static final String PICTURE11      = "image/fenetre.gif";
  private static final String PICTURE12      = "image/cadre.gif";
  private static final String PICTURE13      = "image/piano.gif";
  private static final String PICTURE14      = "image/toilette.gif";
  private static final String PICTURE15      = "image/evier.gif";

  private static final String SEL_PICTURE1       = "image/selBiblio2.gif";
  private static final String SEL_PICTURE2       = "image/selBureau.gif";
  private static final String SEL_PICTURE3       = "image/selChaise4.gif";
  private static final String SEL_PICTURE4       = "image/selTable1.gif";
  private static final String SEL_PICTURE5       = "image/selChaise1.gif";
  private static final String SEL_PICTURE6       = "image/selFauteuil2.gif";
  private static final String SEL_PICTURE7       = "image/selFauteuil3.gif";
  private static final String SEL_PICTURE8       = "image/selStereo.gif";
  private static final String SEL_PICTURE9       = "image/selTv2.gif";
  private static final String SEL_PICTURE10      = "image/selPorte.gif";
  private static final String SEL_PICTURE11      = "image/selFenetre.gif";
  private static final String SEL_PICTURE12      = "image/selCadre.gif";
  private static final String SEL_PICTURE13      = "image/selPiano.gif";
  private static final String SEL_PICTURE14      = "image/selToilette.gif";
  private static final String SEL_PICTURE15      = "image/selEvier.gif";

  private Image backGround  =   para.getImage(BACKGROUND1);
  private Image backGround1 =   para.getImage(BACKGROUND1);
  private Image backGround2 =   para.getImage(BACKGROUND2);
  private Image backGround3 =   para.getImage(BACKGROUND3);
  private Image picture1    =   para.getImage(PICTURE1);
  private Image picture2    =   para.getImage(PICTURE2);
  private Image picture3    =   para.getImage(PICTURE3);
  private Image picture4    =   para.getImage(PICTURE4);
  private Image picture5    =   para.getImage(PICTURE5);
  private Image picture6    =   para.getImage(PICTURE6);
  private Image picture7    =   para.getImage(PICTURE7);
  private Image picture8    =   para.getImage(PICTURE8);
  private Image picture9    =   para.getImage(PICTURE9);
  private Image picture10   =   para.getImage(PICTURE10);
  private Image picture11   =   para.getImage(PICTURE11);
  private Image picture12   =   para.getImage(PICTURE12);
  private Image picture13   =   para.getImage(PICTURE13);
  private Image picture14   =   para.getImage(PICTURE14);
  private Image picture15   =   para.getImage(PICTURE15);

  private Image selPicture1   =   para.getImage(SEL_PICTURE1);
  private Image selPicture2   =   para.getImage(SEL_PICTURE2);
  private Image selPicture3   =   para.getImage(SEL_PICTURE3);
  private Image selPicture4   =   para.getImage(SEL_PICTURE4);
  private Image selPicture5   =   para.getImage(SEL_PICTURE5);
  private Image selPicture6   =   para.getImage(SEL_PICTURE6);
  private Image selPicture7   =   para.getImage(SEL_PICTURE7);
  private Image selPicture8   =   para.getImage(SEL_PICTURE8);
  private Image selPicture9   =   para.getImage(SEL_PICTURE9);
  private Image selPicture10  =   para.getImage(SEL_PICTURE10);
  private Image selPicture11  =   para.getImage(SEL_PICTURE11);
  private Image selPicture12  =   para.getImage(SEL_PICTURE12);
  private Image selPicture13  =   para.getImage(SEL_PICTURE13);
  private Image selPicture14  =   para.getImage(SEL_PICTURE14);
  private Image selPicture15  =   para.getImage(SEL_PICTURE15);

  // PopupMenu
  private static String selectPictures;
  private static String deselectPictures;
  private static String deleteSelectedPictures;
  private static String hideSelectedPictures;
  private static String showPictures;
  private static String increaseSelection;
  private static String decreaseSelection;

  {
    selectPictures =	          para.getT("selectPictures");
    deselectPictures =	        para.getT("deselectPictures");
    deleteSelectedPictures =	  para.getT("deleteSelectedPictures");
    hideSelectedPictures =	    para.getT("hideSelectedPictures");
    showPictures =	            para.getT("showPictures");
    increaseSelection =	        para.getT("increaseSelection");
    decreaseSelection =	        para.getT("decreaseSelection");
  }

 	private static final int SELECTPOP	  = 1;
	private static final int DESELECTPOP  = 2;
  private static final int DELETEPOP	  = 3;
  private static final int HIDEPOP  	  = 4;
	private static final int SHOWPOP      = 5;
  private static final int INCREASEPOP  = 6;
	private static final int DECREASEPOP  = 7;

  private JMenuItem popMenuSelectp	 = new JMenuItem(selectPictures);
  private JMenuItem popMenuDeselectp = new JMenuItem(deselectPictures);
  private JMenuItem popMenuDeletep   = new JMenuItem(deleteSelectedPictures);
  private JMenuItem popMenuHidep		 = new JMenuItem(hideSelectedPictures);
  private JMenuItem popMenuShowp		 = new JMenuItem(showPictures);
  private JMenuItem popMenuIncrease  = new JMenuItem(increaseSelection);
  private JMenuItem popMenuDecrease  = new JMenuItem(decreaseSelection);

  public static final Color DIAGRAM_COLOR = Color.white;
  public static final Dimension DIAGRAM_SIZE = new Dimension(640, 480);

  private Point mousePressed = new Point(0, 0);
  private	AppToolBar appToolBar;
  private JPopupMenu popup = new JPopupMenu();;

  private Diagram() {
    super();
    this.addMouseListener(this);
    this.setBackground(DIAGRAM_COLOR);
    this.setLayout(null);

    // Creation of the popup menu
    popMenuSelectp.setActionCommand("PopSelectp");
    popMenuSelectp.addActionListener(this);
    popMenuDeselectp.setActionCommand("PopDeselectp");
    popMenuDeselectp.addActionListener(this);
    popMenuDeletep.setActionCommand("PopDeletep");
    popMenuDeletep.addActionListener(this);

    popMenuHidep.setActionCommand("PopHidep");
    popMenuHidep.addActionListener(this);
    popMenuShowp.setActionCommand("PopShowp");
    popMenuShowp.addActionListener(this);

    popMenuIncrease.setActionCommand("PopIncrease");
    popMenuIncrease.addActionListener(this);
    popMenuDecrease.setActionCommand("PopDecrease");
    popMenuDecrease.addActionListener(this);

    popup.add(popMenuSelectp);
    popup.add(popMenuDeselectp);
    popup.add(popMenuDeletep);
    popup.addSeparator();
    popup.add(popMenuHidep);
    popup.add(popMenuShowp);
    popup.addSeparator();
    popup.add(popMenuIncrease);
    popup.add(popMenuDecrease);
  }

  public Diagram(AppToolBar anAppToolBar) {
    this();
    this.setAppToolBar(anAppToolBar);
  }

  public AppToolBar getAppToolBar() {
    return appToolBar;
  }

  public void setAppToolBar(AppToolBar anAppToolBar) {
  	appToolBar = anAppToolBar;
  }

  private void addNewPicture(Point aPoint, Image pictureLocation, Image selPictureLocation) {
    Picture newPicture = new Picture(pictureLocation, selPictureLocation);
    newPicture.setLocation(aPoint);
    newPicture.addMouseListener(this);
    newPicture.addMouseMotionListener(this);
    this.add(newPicture);
    this.repaint();
  }

  public void selectAll() {
    Component[] components = this.getComponents();
    Item item;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Item) {
        item = (Item) components[i];
        item.setSelected(true);
      }
    }
    repaint();
  }

  public void deSelectAll() {
    Component[] components = this.getComponents();
    Item item;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Item) {
        item = (Item) components[i];
        item.setSelected(false);
      }
    }
    repaint();
  }

  public void deleteSelection() {
    Component[] components = this.getComponents();
    Item item;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Item) {
        item = (Item) components[i];
        if (item.isSelected()) {
          this.remove(item);
        }
      }
    }
    repaint();
  }

  public void hideSelection() {
    Component[] components = this.getComponents();
    Item item;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Item) {
        item = (Item) components[i];
        if (item.isSelected()) {
          item.setVisible(false);
        }
      }
    }
    repaint();
  }

  public void showPictures() {
    Component[] components = this.getComponents();
    Item item;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Picture) {
        item = (Item) components[i];
          item.setVisible(true);
      }
    }
    repaint();
  }

  public void increaseSelection() {
    Component[] components = this.getComponents();
    Item item;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Item) {
        item = (Item) components[i];
        if (item.isSelected()) {
          item.increase();
        }
      }
    }
    repaint();
  }

  public void decreaseSelection() {
    Component[] components = this.getComponents();
    Item item;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Item) {
        item = (Item) components[i];
        if (item.isSelected()) {
          item.decrease();
        }
      }
    }
    repaint();
  }

  public Image switchBackGround1() {
    backGround = backGround1;
    return backGround;
  }

  public Image switchBackGround2() {
    backGround = backGround2;
    return backGround;
  }

  public Image switchBackGround3() {
    backGround = backGround3;
    return backGround;
  }

  public Dimension getPreferredSize() {
    return DIAGRAM_SIZE;
  }

  protected void paintComponent(Graphics g) {
    g.drawImage(backGround, 0, 0, this.getWidth(), this.getHeight(), this);
    this.repaint();
  }

  public void mousePressed(MouseEvent e) {
    if(e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK){ //right click
	    popup.show(e.getComponent(), e.getX(), e.getY());
    }

    else if(e.getModifiers() == java.awt.event.InputEvent.BUTTON1_MASK){ //left click
     if (e.getSource() == this) {
      if (appToolBar.getSelectedButton() == AppToolBar.SELECT) {
        this.deSelectAll();
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE1B) {
        this.addNewPicture(e.getPoint(), picture1, selPicture1);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE2B) {
        this.addNewPicture(e.getPoint(), picture2, selPicture2);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE3B) {
        this.addNewPicture(e.getPoint(), picture3, selPicture3);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE4B) {
        this.addNewPicture(e.getPoint(), picture4, selPicture4);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE5B) {
        this.addNewPicture(e.getPoint(), picture5, selPicture5);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE6B) {
        this.addNewPicture(e.getPoint(), picture6, selPicture6);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE7B) {
        this.addNewPicture(e.getPoint(), picture7, selPicture7);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE8B) {
        this.addNewPicture(e.getPoint(), picture8, selPicture8);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE9B) {
        this.addNewPicture(e.getPoint(), picture9, selPicture9);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE10B) {
        this.addNewPicture(e.getPoint(), picture10, selPicture10);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE11B) {
        this.addNewPicture(e.getPoint(), picture11, selPicture11);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE12B) {
        this.addNewPicture(e.getPoint(), picture12, selPicture12);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE13B) {
        this.addNewPicture(e.getPoint(), picture13, selPicture13);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE14B) {
        this.addNewPicture(e.getPoint(), picture14, selPicture14);
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.PICTURE15B) {
        this.addNewPicture(e.getPoint(), picture15, selPicture15);
      }
     }

     else if (e.getSource() instanceof Item) {
      if (appToolBar.getSelectedButton() == AppToolBar.SELECT) {
        Item clickedItem = (Item) (e.getSource());
        clickedItem.setSelected(!(clickedItem.isSelected()) );
        mousePressed = e.getPoint();
        repaint();
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.INCREASE) {
        Item clickedItem = (Item) (e.getSource());
        clickedItem.increase();
        mousePressed = e.getPoint();
        repaint();
      }
      else if (appToolBar.getSelectedButton() == AppToolBar.DECREASE) {
        Item clickedItem = (Item) (e.getSource());
        clickedItem.decrease();
        mousePressed = e.getPoint();
        repaint();
     }
     }
    }
  }

  public void mouseDragged(MouseEvent e) {
    if (appToolBar.getSelectedButton() == AppToolBar.SELECT) {
      if (e.getSource() instanceof Item) {
        Item item = (Item) e.getSource();
        // item location (x, y) and size (r) before the item has been dragged
        Rectangle r = item.getBounds();
        int x = r.x + e.getX() - mousePressed.x;
        int y = r.y + e.getY() - mousePressed.y;
        if (x < 0) x = 0;
        if (y < 0) y = 0;
        item.setLocation(x, y);
      }
    }
  }

  // Popup menu command intercept
  private int getCommand(String aCommand) {
    int	action = -1;
    if      (aCommand.equals("PopSelectp"))  action = SELECTPOP;
    else if (aCommand.equals("PopDeselectp"))action = DESELECTPOP;
    else if (aCommand.equals("PopDeletep"))  action = DELETEPOP;
    else if (aCommand.equals("PopHidep"))    action = HIDEPOP;
    else if (aCommand.equals("PopShowp"))    action = SHOWPOP;
    else if (aCommand.equals("PopIncrease")) action = INCREASEPOP;
    else if (aCommand.equals("PopDecrease")) action = DECREASEPOP;
    return action;
  }

  // Popup menu action
  public void actionPerformed(ActionEvent e) {
 	String popCommand = e.getActionCommand();
   switch (this.getCommand(popCommand)) {
      case SELECTPOP:   this.selectAll();
                        break;
    	case DESELECTPOP: this.deSelectAll();
                        break;
      case DELETEPOP:   this.deleteSelection();
                        break;
      case HIDEPOP:     this.hideSelection();
                        break;
    	case SHOWPOP:     this.showPictures();
                        break;
      case INCREASEPOP: this.increaseSelection();
                        break;
    	case DECREASEPOP: this.decreaseSelection();
                        break;
    }
  }

  public void mouseClicked(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mouseMoved(MouseEvent e) {}

}
